﻿using Newtonsoft.Json;
using SYH.Domain;
using SYH.Domain.ViewModels.Applicaton;
using SYH.Web.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SYH.Web.Controllers
{
    public class PubController : Controller
    {
        // GET: Pub
        /// <summary>
        /// 重写json格式化输出
        /// </summary>
        /// <param name="data"></param>
        /// <param name="contentType"></param>
        /// <param name="contentEncoding"></param>
        /// <param name="behavior"></param>
        /// <returns></returns>
        protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new MyJsonResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior
            };
        }

        /// <summary>
        /// 重写json格式化输出
        /// </summary>
        /// <param name="data"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        protected JsonResult MyJson(object data, string contentType)
        {
            return new MyJsonResult
            {
                Data = data,
                ContentType = contentType,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 重写json格式化输出
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected JsonResult MyJson(object data)
        {
            return new MyJsonResult
            {
                Data = data,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }


        /// <summary>
        /// 系统错误异常处理
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnException(ExceptionContext filterContext)
        {
            // 此处进行异常记录，可以记录到数据库或文本，也可以使用其他日志记录组件。
            // 通过filterContext.Exception来获取这个异常。
            if (filterContext.Exception != null)
            {
                var error = filterContext.Exception.ToString();
                var msg = RouteData.Values["controller"].ToString() + "." + RouteData.Values["action"].ToString() + "\r\n" + error;
                Log("系统错误", msg);
                Response.StatusCode = 500;
                Response.Write(JsonConvert.SerializeObject(new ResultVM(99, filterContext.Exception.Message)));
                Response.End();
            }
            else
            {
                // 执行基类中的OnException
                base.OnException(filterContext);
            }
        }

        /// <summary>
        /// 输出分页数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public ActionResult Table(object data, int count)
        {
            Hashtable hash = new Hashtable();
            hash["Data"] = data;
            hash["Count"] = count;
            return MyJson(hash);
        }

        /// <summary>
        /// 输出表格数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public ActionResult Table<T>(IEnumerable<T> data)
        {
            Hashtable hash = new Hashtable();
            hash["Data"] = data;
            hash["Count"] = data != null ? data.Count() : 0;
            return MyJson(hash);
        }

        /// <summary>
        /// 输出表格数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public ActionResult Table(DataTable data)
        {
            Hashtable hash = new Hashtable();
            hash["Data"] = data;
            hash["Count"] = data.Rows.Count;
            return MyJson(hash);
        }


        /// <summary>
        /// 返回json格式数据
        /// </summary>
        /// <returns></returns>
        public ActionResult JsonData(object data)
        {
            return MyJson(data);
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        /// <returns></returns>
        public ActionResult Success()
        {
            return MyJson(new ResultVM(0, ""));
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        /// <returns></returns>
        public ActionResult Success(string m)
        {
            return MyJson(new ResultVM(0, m));
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        /// <returns></returns>
        public ActionResult Success(object data)
        {
            var res = new ResultVM(0, "");
            res.data = data;
            return MyJson(res);
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        /// <returns></returns>
        public ActionResult Success(object data, string m)
        {
            var res = new ResultVM(0, m);
            res.data = data;
            return MyJson(res);
        }

        /// <summary>
        /// 只输出错误不提示错误信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Error()
        {
            Response.StatusCode = 500;
            return Json(new ResultVM(99, "系统发生错误！"), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 输出自定义级别提示信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name = "r" >自定义级别99为系统级别</ param >
        /// <returns></returns>
        public ActionResult Error(string msg, int r)
        {
            return Json(new ResultVM(r, msg), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 输出并记录错误信息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ActionResult Error(string msg)
        {
            HandleErrorInfo ei = new HandleErrorInfo(new Exception(msg), RouteData.Values["controller"].ToString(), RouteData.Values["action"].ToString());
            Log("一般性错误", ei.Exception.ToString());
            Response.StatusCode = 500;
            return Json(new ResultVM(99, msg), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 输出错误信息并记录错误异常
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public ActionResult Error(Exception e)
        {
            HandleErrorInfo ei = new HandleErrorInfo(e, RouteData.Values["controller"].ToString(), RouteData.Values["action"].ToString());
            string msg = ei.Exception.ToString();
            if (e.InnerException != null)
            {
                msg = e.InnerException.Message;
            }
            Log("系统错误", msg);
            Response.StatusCode = 500;
            return Json(new ResultVM(99, msg), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 输出错误信息并记录错误异常
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public ActionResult Error(string msg, Exception e)
        {
            HandleErrorInfo ei = new HandleErrorInfo(e, RouteData.Values["controller"].ToString(), RouteData.Values["action"].ToString());
            Log("一般错误", ei.Exception.ToString());
            Response.StatusCode = 500;
            return Json(new ResultVM(99, msg), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="name"></param>
        /// <param name="body"></param>
        protected virtual void Log(string name, string body)
        {
            Log(name, "系统错误", body);
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="name"></param>
        /// <param name="body"></param>
        protected virtual void Log(string name, string title, string body)
        {
            var wl = ConfigurationManager.AppSettings["writeLog"];
            if (wl == "true")
            {
                fmtx.Framework.Log.Warning(title + "：" + name + ",内容：" + body);
            }
        }

        /// <summary>
        /// 记录源对象信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="bean"></param>
        protected virtual void Log(string name, BaseEntity bean)
        {
            fmtx.Framework.Log.Warning("操作：" + name + ",源数据:" + JsonConvert.SerializeObject(bean));
        }
    }
}